// pages/home/home.js
import drawQrcode from "../../utils/weapp.qrcode.min.js"
import api from '../../Sendingmethod/Sendingmethod'
import encrypt from "../../utils/crypto-util.js"
Page({

    /**
     * 页面的初始数据
     */
    data: {
        //底部导航栏 index
        tabarindex: "pages/promotion/promotion",
        userinfo: '',
        statusBar_H: '',
        menu_Info: "",
        //收益text
        shouyiarr: [
            '总收益',
            '今日收益',
            '团队收益',
            '商家收益'
        ],
        imgboxone: "",
        pmobj: {},
        triggered:true
    },

    /**
     * 生命周期函数--监听页面加载
     */
    onLoad(options) {
        console.log(options, '首页传入数据');

        //获取状态栏高度
        const statusBarHeight = wx.getWindowInfo().statusBarHeight;
        //获取胶囊属性
        const menuButtonInfo = wx.getMenuButtonBoundingClientRect();
        //计算导航栏属性
        const navBarHeight = menuButtonInfo.height + (menuButtonInfo.top - statusBarHeight) * 2
        //赋值给 高度 变量
        this.setData({
            statusBar_H: (statusBarHeight + navBarHeight) * 2,
            menu_Info: statusBarHeight * 2
        })
        console.log(statusBarHeight, navBarHeight, this.statusBar_H, '页面刷新');


    },
    /**
     * 
     * 获取到当前用户的推广信息
     */
    async goutofun() {
        const query = wx.createSelectorQuery()
        let _that = this
        let res = await api.promoteindex()
        console.log(res, '代理商信息');
        if (res.code == 0) {
            // let allnum = res.data.amount_month + res.data.amount_today + res.data.amount_total
            const allnum = ['amount_month', 'amount_today', 'amount_total']
                .reduce((sum, key) => {
                    const value = Number(res.data?.[key]) || 0 // 安全转换非数字和空值
                    return sum + value
                }, 0) // 初始值设为0
            this.setData({
                pmobj: {
                    ...res.data,
                    allnum: allnum
                }
            })
            query.select('#myQrcode')
                .fields({
                    node: true,
                    size: true
                })
                .exec((res1) => {
                    console.log(res1);
                    var canvas = res1[0].node
                    drawQrcode({
                        canvas: canvas,
                        canvasId: 'myQrcode',
                        width: 270,
                        padding: 20,
                        background: '#ffffff',
                        foreground: '#000000',
                        text: res.data.share_url,
                    })

                    // 获取临时路径（得到之后，想干嘛就干嘛了）
                    wx.canvasToTempFilePath({
                        canvasId: 'myQrcode',
                        canvas: canvas,
                        x: 0,
                        y: 0,
                        width: 270,
                        height: 260,
                        destWidth: 200,
                        destHeight: 200,
                        success(res) {
                            console.log('二维码临时路径：', res.tempFilePath)
                            _that.setData({
                                imgboxone: res.tempFilePath
                            })
                        },
                        fail(res) {
                            console.error(res)
                        }
                    })
                })
        } else {

        }
    },





    /**
     * 跳转
     */
    goto(e) {
        console.log(e.target.dataset.index);
        if (e.target.dataset.index == '1') {
            wx.navigateTo({
                url: '/pages/Payouts/Payouts',
            })
        }
        if (e.target.dataset.index == '2') {
            wx.navigateTo({
                url: '/pages/dmy_team/dmy_team'
            })
        }

    },
    /**
     * 生命周期函数--监听页面初次渲染完成
     */
    onReady() {

    },
    /**
     * 选择
     */
    funbrnon() {
        // console.log('111');

    },

    /**
     * 生命周期函数--监听页面显示
     */
    onShow() {
        //获取用户信息
        if (wx.getStorageSync('userinfo')) {
            console.log('存在用户信息');
            let data = wx.getStorageSync('userinfo')
            this.setData({
                userinfo: JSON.parse(data)
            })

        }
        this.goutofun()


    },
    /**
     * 
     * 保存二维码切图
     */
    downloadCode: function (res) {
        var filePath = this.data.imgboxone
        console.log('下载中' + filePath)
        wx.showLoading({
            title: '保存中',
            mask: true
        })
        wx.saveImageToPhotosAlbum({
            filePath: filePath,
            success: function (res) {
                wx.hideLoading()
                wx.showToast({
                    title: '图片保存成功',
                    icon: 'success',
                    duration: 2000 //持续的时间
                })
            },
            fail: function (err) {
                wx.hideLoading()
                console.log(err)
                wx.showToast({
                    title: '图片保存失败',
                    icon: 'none',
                    duration: 2000 //持续的时间
                })
            }
        })
    },
    xialshuax() {
        wx.showLoading({
          mask:true
        })
        console.log('下拉刷新触发');
        this.goutofun()
        setTimeout(()=>{
            wx.hideLoading()
            this.setData({
                triggered:false
            })
        },1000)
    },
    /**
     * 复制链接
     */
    copylink() {
        wx.setClipboardData({
            data: this.data.pmobj.share_url,
            success(res) {
                wx.getClipboardData({
                    success(res) {
                        console.log(res.data) // data
                    }
                })
            }
        })
    },


    /**
     * 生命周期函数--监听页面隐藏
     */
    onHide() {

    },

    /**
     * 生命周期函数--监听页面卸载
     */
    onUnload() {

    },

    /**
     * 页面相关事件处理函数--监听用户下拉动作
     */
    onPullDownRefresh() {
       
      
    },

    /**
     * 页面上拉触底事件的处理函数
     */
    onReachBottom() {

    },

    /**
     * 用户点击右上角分享
     */
    onShareAppMessage() {

    }
})